package com.jeff.tianti.controller;

import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.common.util.ExcelUtils;
import com.jeff.tianti.org.dto.PatientQueryDTO;
import com.jeff.tianti.org.entity.Patient;
import com.jeff.tianti.org.entity.Role;
import com.jeff.tianti.org.entity.User;
import com.jeff.tianti.org.service.PatientService;
import com.jeff.tianti.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author guost
 * @Date 2019/2/9 17:57
 **/
@Controller
@RequestMapping("/patient")
public class PatientController {
    @Resource
    private PatientService patientService;

    @RequestMapping(value="/addPatient",method = RequestMethod.POST)
    public String addPatient(Patient patient) {
        System.out.println("获得的参数：" + patient.toString());
        Patient savePatient = patientService.save(patient);
        System.out.println("保存后的ID=" + savePatient.getId());
        return "redirect:/patientIndex";
    }

    @RequestMapping("/list")
    public String list(HttpServletRequest request, Model model){
        String userName = request.getParameter("userName");
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");

        int currentPage = 1;
        int pageSize = 10;
        if(StringUtils.isNotBlank(currentPageStr)){
            currentPage = Integer.parseInt(currentPageStr);
        }
        if(StringUtils.isNotBlank(pageSizeStr)){
            pageSize = Integer.parseInt(pageSizeStr);
        }

        PatientQueryDTO patientQueryDTO = new PatientQueryDTO();
        patientQueryDTO.setName(userName);
        patientQueryDTO.setCurrentPage(currentPage);
        patientQueryDTO.setPageSize(pageSize);

        PageModel<Patient> page = patientService.queryPatientPage(patientQueryDTO);
        model.addAttribute("page", page);
        model.addAttribute("patientQueryDTO", patientQueryDTO);
        model.addAttribute(Constants.MENU_NAME, Constants.MENU_PATIENT_LIST);

        return "patient/patient_list";
    }

    /**
     * 导出患者数据
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/export")
    public String exportOrder(HttpServletRequest request, HttpServletResponse response){
        String userName = request.getParameter("userName");
        Map<String, Object> params = new HashMap<String, Object>();
        if(StringUtils.isNotBlank(userName)) {
            params.put("username", userName);
        }

        List<Patient> patientList = patientService.getAll();
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        Map<String, String> headNameMap = new LinkedHashMap<String, String>();
        headNameMap.put("name", "患者姓名");
        headNameMap.put("sex", "姓别");
        headNameMap.put("nation", "民族");
        headNameMap.put("age", "年龄");
        headNameMap.put("identityCard", "身份证号");
        headNameMap.put("mobile", "手机号");
        headNameMap.put("qq", "QQ");
        headNameMap.put("email", "Email");
        headNameMap.put("wechat", "微信");
        headNameMap.put("patientDesc", "病情详述");
        headNameMap.put("createDate","创建时间");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(patientList != null && patientList.size() > 0){
            for(Patient patient : patientList){
                String createDate = "";

                if(patient.getCreateDate() != null){
                    createDate = sdf.format(patient.getCreateDate());
                }

                Map<String,Object> map = new HashMap<String,Object>();
                map.put("name", patient.getName());
                map.put("sex", patient.getSex());
                map.put("nation", patient.getNation());
                map.put("age", patient.getAge());
                map.put("identityCard", patient.getIdentityCard());
                map.put("mobile", patient.getMobile());
                map.put("qq", patient.getQq());
                map.put("email", patient.getEmail());
                map.put("wechat", patient.getWechat());
                map.put("patientDesc", patient.getPatientDesc());
                map.put("createDate",createDate);

                list.add(map);
            }
        }

        ExcelUtils.exportXlsx(response, "患者数据", headNameMap, list);
        return null;
    }
}
